<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>sum 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-metrics-sum-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-sum-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-sum-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-metrics-sum-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-metrics-sum-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-metrics.html">度量聚合</a></span>
»
<span class="breadcrumb-node">sum 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-metrics-string-stats-aggregation.html">« string_stats 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-top-hits-aggregation.html">top_hits 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-metrics-sum-aggregation"></a>sum 聚合
</h2>
</div></div></div>
<p>
一个 <code class="literal">single-value</code>(单值) 度量聚合，对从聚合文档中提取的数值进行汇总。

这些值可以从文档中指定的 numeric 字段中提取，也可以由提供的脚本生成。
</p>
<p>
假设数据由代表销售记录的文档组成，可以用以下公式计算所有帽子的销售价格(hat_prices)：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "match" : { "type" : "hat" }
            }
        }
    },
    "aggs" : {
        "hat_prices" : { "sum" : { "field" : "price" } }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/391.console"></div>
<p>结果是：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "hat_prices": {
           "value": 450.0
        }
    }
}</pre>
</div>
<p>聚合的名称(上面的<code class="word">hat_prices</code>)也用作 key，通过它可以从返回的响应中检索聚合结果。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_script_13"></a>脚本
</h3>
</div></div></div>
<p>还可以使用一个脚本来获取销售价格：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "match" : { "type" : "hat" }
            }
        }
    },
    "aggs" : {
        "hat_prices" : {
            "sum" : {
                "script" : {
                   "source": "doc.price.value"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/392.console"></div>
<p>
这将把 <code class="literal">script</code> 参数解释为一个<code class="literal">inline</code>(内联) 脚本，使用 <code class="literal">painless</code>(无痛) 脚本语言，没有脚本参数。

要使用存储的脚本，请使用以下语法:
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "match" : { "type" : "hat" }
            }
        }
    },
    "aggs" : {
        "hat_prices" : {
            "sum" : {
                "script" : {
                    "id": "my_script",
                    "params" : {
                        "field" : "price"
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/393.console"></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="_value_script_7"></a>值脚本(value script)
</h4>
</div></div></div>
<p>
也可以使用<code class="literal">_value</code>从脚本中访问字段值。

例如，下面的查询计算所有帽子的价格平方的和：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "match" : { "type" : "hat" }
            }
        }
    },
    "aggs" : {
        "square_hats" : {
            "sum" : {
                "field" : "price",
                "script" : {
                    "source": "_value * _value"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/394.console"></div>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_missing_value_12"></a>缺失的值
</h3>
</div></div></div>
<p>
参数 <code class="literal">missing</code> 定义应该如何处理有缺失值的文档。

默认情况下，它们会被忽略，但也可以将它们视为有一个(默认)值。

例如，下面的查询将所有销售的不带价格的帽子的价格视为 <code class="literal">100</code>。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "match" : { "type" : "hat" }
            }
        }
    },
    "aggs" : {
        "hat_prices" : {
            "sum" : {
                "field" : "price",
                "missing": 100 <a id="CO208-1"></a><i class="conum" data-value="1"></i>
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/395.console"></div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-metrics-string-stats-aggregation.html">« string_stats 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-top-hits-aggregation.html">top_hits 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>